package com.sixfoot.facade.user;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sixfoot.dao.RoleRepository;
import com.sixfoot.domain.Role;
import com.sixfoot.domain.User;
import com.sixfoot.foundation.mapping.adapter.AbstractDtoAdapter;

@Component
public class UserAdapter extends AbstractDtoAdapter<User, UserDto, UserInputArgs>  {
	@Autowired
	private RoleRepository roleRepository;

	@Override
	public User postConvertToDo(UserInputArgs i, User d) {
		return postUpdate(i, d);
	}

	@Override
	public User postUpdate(UserInputArgs i, User d) {
		List<String> roleIds = i.getRoleIds();
		if (roleIds != null) {
			Set<Role> roles = new HashSet<>();
			for (String roleId : roleIds) {
				Role role = roleRepository.findOne(roleId);
				roles.add(role);
			}
			d.setRoles(roles);
		}
		return d;
	}

}
